package com.jack.database_exp.controller;
import com.alibaba.fastjson.JSONObject;
import com.jack.database_exp.bean.*;
import com.jack.database_exp.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@SuppressWarnings("all")
@Controller
public class BuyController{
    @Autowired
    OrdersMapper ordersMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    SellInfoMapper sellInfoMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    BuyMapper buyMapper;
    @Autowired
    BuyInfoMapper buyInfoMapper;
    @Autowired
    WorkerMapper workerMapper;
    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    PlanInfoMapper planInfoMapper;
    @Autowired
    PlanMapper planMapper;
    @RequestMapping("/buy")
    public String buy(HttpServletRequest request, HttpServletResponse response, @RequestParam("CName") String CName, @RequestParam("CTel") String CTel){
        HttpSession session = request.getSession();
        boolean flag = false;
        List<Customer> customers = customerMapper.queryCustomers();
        for (Customer worker : customers){
            if(worker.getCName().equals(CName)&&worker.getCTel().equals(CTel)){
                flag = true;
                break;
            }
        }
        if(flag){
            session.setAttribute("loginCustomer",CName);
            session.setMaxInactiveInterval(60*60);
            return "redirect:/customerBuy";
        }else{
            request.setAttribute("msg","密码错误");
            return "/customerLogin";
        }
    }

    @RequestMapping("/showPlans")
    public ModelAndView showPlans(){
        List<Plan> plans = planMapper.queryPlans();
        List<PlanAndInfos> planAndInfosList = new ArrayList<>();
        for (Plan plan : plans) {
            PlanAndInfos planAndInfos = new PlanAndInfos();
            planAndInfos.setPlan(plan);
            planAndInfos.setPlanInfoList(planInfoMapper.queryPlanInfoByPNo(plan.getPNo()));
            planAndInfos.setName(workerMapper.queryWorkerById(plan.getWNo()));
            planAndInfosList.add(planAndInfos);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("planAndInfosList",planAndInfosList);
        modelAndView.setViewName("showPlans");
        return modelAndView;
    }
    @RequestMapping("/editBuy")
    public ModelAndView editBuy(Integer PNo){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("editBuy");
        Plan plan = planMapper.queryPlanById(PNo);
        System.out.println(plan.getPDate());
        List<PlanInfo> planInfos = planInfoMapper.queryPlanInfoByPNo(PNo);
        modelAndView.addObject("plan",plan);
        modelAndView.addObject("planInfos",planInfos);
        return modelAndView;
    }
    @RequestMapping("/showBuy")
    public ModelAndView showBuy(@RequestBody String json){
        String[] split = json.split(",\"t\",");
        json = "";
        for (int i=0;i<split.length-1;i++) {
            System.out.println(split[i]);
            json+=split[i];
            if(i!=split.length-2){
                json+=",";
            }
        }
        json+="]";
        String last = split[split.length-1].substring(0,split[split.length-1].length()-1);
        List<PlanMidBean> planMidBeans = JSONObject.parseArray(json, PlanMidBean.class);
        Remark remark = JSONObject.parseObject(last, Remark.class);
        Buy buy = new Buy();
        Date date = new Date();
        date.setYear(remark.getYear()-1900);
        date.setMonth(remark.getMonth()-1);
        buy.setBDate(date);
        buy.setPNo(Integer.parseInt(remark.getWorker()));
        double total = 0;
        for (PlanMidBean planMidBean : planMidBeans) {
            total+=planMidBean.getNum()*planMidBean.getPrice();
        }
        buy.setBTotal(total);
        buyMapper.insertBuy(buy);
        for (PlanMidBean planMidBean : planMidBeans) {
            BuyInfo buyInfo = new BuyInfo();
            buyInfo.setBINum(planMidBean.getNum());
            buyInfo.setBIPrice(planMidBean.getPrice());
            buyInfo.setGNo(planMidBean.getId());
            goodsMapper.updateGoodsById(planMidBean.getNum(),planMidBean.getId());
            buyInfo.setBNo(buy.getBNo());
            buyInfoMapper.insertBuyInfo(buyInfo);
        }
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("buy",buy);
        modelAndView.addObject("buyInfos",planMidBeans);
        String buy_JSON = JSONObject.toJSONString(buy);
        String buyInfos_JSON = JSONObject.toJSONString(planMidBeans);
        redisTemplate.boundValueOps("buyInfos_JSON").set(buyInfos_JSON);
        redisTemplate.boundValueOps("buy_JSON").set(buy_JSON);
        modelAndView.setViewName("showBuy");
        return modelAndView;
    }
    @RequestMapping("/lastBuy")
    public ModelAndView showPlan(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showBuy");
        String buyInfos_JSON = redisTemplate.boundValueOps("buyInfos_JSON").get();
        String buy_JSON = redisTemplate.boundValueOps("buy_JSON").get();
        Buy buy = JSONObject.parseObject(buy_JSON,Buy.class);
        List<PlanMidBean> planMidBeans = JSONObject.parseArray(buyInfos_JSON, PlanMidBean.class);
        modelAndView.addObject("buy",buy);
        modelAndView.addObject("buyInfos",planMidBeans);
        return modelAndView;
    }

    @RequestMapping("/money")
    public @ResponseBody Bean getMoney(){
        List<BuyInfo> buyInfos = buyInfoMapper.queryBuyInfos();
        List<SellInfo> sellInfos = sellInfoMapper.querySellInfos();
        double total = 0;
        int num = 0;
        for (BuyInfo buyInfo : buyInfos) {
            total-=buyInfo.getBINum()*buyInfo.getBIPrice();
        }
        for (SellInfo sellInfo : sellInfos) {
            total+=sellInfo.getSINum()*sellInfo.getSIPrice();
            num+=sellInfo.getSINum();
        }
        Bean bean = new Bean();
        bean.setMoney(total);
        bean.setNum(num);
        return bean;
    }
    @RequestMapping("/remind")
    public @ResponseBody List<Goods> remind(){
        return goodsMapper.queryRemind();
    }
    @RequestMapping("/showBuys")
    public ModelAndView showBuys(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("showBuys");
        List<Buy> buys = buyMapper.queryBuys();
        List<BuyAndInfos> buyAndInfosList = new ArrayList<>();
        for (Buy buy:buys){
            BuyAndInfos buyAndInfos = new BuyAndInfos();
            buyAndInfos.setBuy(buy);
            buyAndInfos.setBuyInfoList(buyInfoMapper.queryBuyInfoByBNo(buy.getBNo()));
            buyAndInfosList.add(buyAndInfos);
        }
        modelAndView.addObject("buyAndInfosList",buyAndInfosList);
        modelAndView.setViewName("showBuys");
        return modelAndView;
    }
    @RequestMapping("/showOrders")
    public ModelAndView showOrders(ModelAndView modelAndView){
        List<Orders> orders = ordersMapper.queryOrders();
        for (Orders order : orders) {
            System.out.println(order);
        }
        List<Orders> my = new ArrayList<>();
        for (Orders orders1 : orders) {
            if(!orders1.getStatus()){
                my.add(orders1);
            }
        }
        modelAndView.addObject("orders",my);
        modelAndView.setViewName("showOrders");
        return modelAndView;
    }
}
